//
//  KTDataController.m
//  KTChat
//
//  Created by Kevin on 2016/11/12.
//  Copyright © 2016年 Kevin. All rights reserved.
//

#import "KTDataController.h"

@interface KTDataController ()

@end

// 常量
static NSString *const KTDataControllerFolderName = @"KTChat";
static NSString *const KTDataControllerDatabaseName = @"KTChatDatabase";
static NSString *const KTDataControllerSendingMessageIdKey = @"KTDataControllerSendingMessageIdKey";
static NSString *const KTDataControllerSendFailedMessageIdKey = @"KTDataControllerSendFailedMessageIdKey";

@implementation KTDataController

+ (instancetype)sharedController
{
    static KTDataController *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[KTDataController alloc] init];
    });
    
    return instance;
}

- (instancetype)init
{
    if (self = [super init]) {
        NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        _chatFolder = [doc stringByAppendingPathComponent:KTDataControllerFolderName];
        NSFileManager *manager = [NSFileManager defaultManager];
        if (![manager fileExistsAtPath:_chatFolder]) {
            [manager createDirectoryAtPath:_chatFolder withIntermediateDirectories:YES attributes:nil error:nil];
        }
        _audiosFolder = [_chatFolder stringByAppendingPathComponent:@"audios"];
        if (![manager fileExistsAtPath:_audiosFolder]) {
            [manager createDirectoryAtPath:_audiosFolder withIntermediateDirectories:YES attributes:nil error:nil];
        }
        _videosFolder = [_chatFolder stringByAppendingPathComponent:@"videos"];
        if (![manager fileExistsAtPath:_videosFolder]) {
            [manager createDirectoryAtPath:_videosFolder withIntermediateDirectories:YES attributes:nil error:nil];
        }
        _photosFolder = [_chatFolder stringByAppendingPathComponent:@"photos"];
        if (![manager fileExistsAtPath:_photosFolder]) {
            [manager createDirectoryAtPath:_photosFolder withIntermediateDirectories:YES attributes:nil error:nil];
        }
    }
    
    return self;
}

#pragma mark -- setter --

- (void)setDatabaseUserId:(long long)databaseUserId
{
    NSAssert(databaseUserId > 0, @"databaseUserId必须为正数");
    _databaseUserId = databaseUserId;
    [self setupDatabase];
}

#pragma mark -- 数据库相关 --

- (void)setupDatabase
{
}

@end
